﻿using BookManege.entity;
using BookManege.service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Drawing.Imaging;

namespace BookManege.controller
{
    public partial class enterBook : Form
    {
        private BookManegeService bookManegeService;
        private MyHttpRespons myHttpRespons;
        private Byte[] bytes;
        public enterBook()
        {
            InitializeComponent();
            this.bookManegeService = new BookManegeService();
            this.myHttpRespons = new MyHttpRespons();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                errorProvider1.SetError(textBox1, "不能为空");
            }
            else if (textBox2.Text == "")
            {
                errorProvider1.Clear();
                errorProvider1.SetError(textBox2, "不能为空");
            }
            else if (textBox3.Text == "")
            {
                errorProvider1.Clear();
                errorProvider1.SetError(textBox3, "不能为空");
            }
            else if (textBox4.Text == "")
            {
                errorProvider1.Clear();
                errorProvider1.SetError(textBox4, "不能为空");
            }
            else if (textBox5.Text == "")
            {
                errorProvider1.Clear();
                errorProvider1.SetError(textBox5, "不能为空");
            }
            else
            {
                errorProvider1.Clear();
                string imgurl = DateTime.Now.ToString("yyyyMMddHHmmss");
                string path = Program.bookPath + imgurl;
                Book book = new()
                {
                    BookName = textBox1.Text,
                    Aurher = textBox2.Text,
                    Icbn = textBox3.Text,
                    Price = Convert.ToDouble(textBox4.Text),
                    Number = Convert.ToInt32(textBox5.Text),
                    ImgUrl = imgurl
                };
                if (bookManegeService.InsertBook(book) > 0)
                {
                    File.WriteAllBytes(path + ".jpg", bytes);
                    label1.Text = "添加成功！";
                    label7.Text = "";
                }
                else
                {
                    label1.Text = "添加失败！";
                }
            }
        }

        private async void button2_Click(object sender, EventArgs e)
        {
                label7.Text = "正在网络查询，请稍后...";
                string data = await myHttpRespons.GetResponseBook(textBox3.Text);
                if (data != null)
                {
                    JObject jsonObject = JObject.Parse(data);
                    if ((int)jsonObject["code"] != 1)
                    {
                        label7.Text = "查询失败，请手动输入";
                    }
                    else
                    {
                        JToken? book = jsonObject["data"];
                        string imgurl = book["img"].ToString();
                        bytes = await myHttpRespons.LoadImageFromUrlAsync(imgurl);
                        pictureBox1.Image = GetImg();
                        textBox1.Text = (string)book["title"];
                        textBox2.Text = (string)book["author"];
                        textBox4.Text = (string)book["price"];
                        label7.Text = "查询成功！";
                }
                }
        }
        private async void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text.Length > 10)
            {
                label7.Text = "正在网络查询，请稍后...";
                string data = await myHttpRespons.GetResponseBook(textBox3.Text);
                if (data != null)
                {
                    JObject jsonObject = JObject.Parse(data);
                    if ((int)jsonObject["code"] != 1)
                    {
                        label7.Text = "查询失败，请手动输入";
                    }
                    else
                    {
                        label7.Text = "查询成功！";
                        JToken? book = jsonObject["data"];
                        string imgurl = book["img"].ToString();
                        bytes = await myHttpRespons.LoadImageFromUrlAsync(imgurl);
                        pictureBox1.Image = GetImg();
                        textBox1.Text = (string)book["title"];
                        textBox2.Text = (string)book["author"];
                        textBox4.Text = (string)book["price"];
                    }
                }
            }
        }
        //展示图片
        public Bitmap GetImg()
        {
            Image image= Image.FromStream(new MemoryStream(bytes));
            int newHeight = 170;
            float originalAspectRatio = (float)image.Width / image.Height;
            int newWidth = (int)(newHeight * originalAspectRatio);
            Bitmap resultImg = new (newWidth, newHeight);
            using (Graphics graphics = Graphics.FromImage(resultImg))
            {
                // 设置高质量插值法  
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                // 绘制原始图像到新创建的Bitmap上  
                graphics.DrawImage(image, 0, 0, newWidth, newHeight);
            }
            return resultImg;
        }
    }
}
